package com.service.impl;

import com.constant.Constant;
import com.dao.CourseDao;
import com.dao.CourseTypeDao;
import com.entity.Course;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.service.CourseService;
import com.service.CourseTypeService;
import com.vo.CourseVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 * date:2021/4/29
 * description:
 * Author:Stargazer
 */
@Service
public class CourseServiceImpl implements CourseService {
    @Autowired
    private CourseDao courseDao;
    @Autowired
    CourseTypeDao courseTypeDao;

    @Override
    public List<Course> findThreeCourseByCourseId(Integer course_id) {
        List<Course> courses = courseDao.selectThreeHotCourseByCourseId(course_id);
        return courses;
    }





    @Override
    public PageInfo<CourseVo> findCourseByCourseVo(CourseVo courseVo) {
        String author = courseVo.getAuthor();
        String course_name = courseVo.getCourse_name();
        Integer pageNo = courseVo.getPageNo();
        Integer status = courseVo.getStatus();
        Integer course_type_id = courseVo.getCourse_type_id();
        Date begin_date = courseVo.getBegin_date();
        Date end_date = courseVo.getEnd_date();
        PageHelper.startPage(pageNo, Constant.PAGE_SIZE);
        List<CourseVo> courseVos = courseDao.selectCourseByCourseVo(author, course_name, status, course_type_id, begin_date, end_date);
        return new PageInfo<CourseVo>(courseVos);
    }

    @Transactional
    @Override
    public int modifyCourseById(Integer id, Integer status) {
        int i = courseDao.updateCourseById(id, status);
        return i;
    }
    @Transactional
    @Override
    public int addCourseByCondition(Course course) {
        int i = courseDao.insertCourseByCondition(course);
        return i;
    }

    @Override
    public Course findCourseById(Integer id){
        Course course = courseDao.selectCourseById(id);
        return course;
    }

    @Transactional
    @Override
    public int modifyCourseByCondition(Course course) {

        int i = courseDao.updateCourseByCondition(course);
        return i;
    }
//====张
    @Override
    public List<Course> findTopByGroup() {
        List<Course> courses = courseDao.selectAllCourses();
        List list = new ArrayList();
        Set ids = new HashSet();
        for (Course course : courses) {
            Integer id = course.getCourseType().getParent_id();
            Integer type1id = courseTypeDao.selectParentIdById(id);
            course.setCourse_type_id(type1id);
            ids.add(type1id);
        }
        for (Object id:ids) {
            List sunList = new ArrayList();
            for (int i = 0; i < courses.size() ; i++) {
                if (courses.get(i).getCourse_type_id()==id && sunList.size()<4){
                    sunList.add(courses.get(i));
                }
            }
            list.addAll(sunList);
        }
        return list;
    }
    //====张
}
